<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box1"></div>
    <div id="app">
        <p>hello,node</p>
        <img src="../image/logo1.png" alt="">
        <!--这是一个注释-->
        <h1>标题1</h1>
        hello world!
    </div>
    <div class="box2">box2</div>
</body>
<script>
    let app = document.getElementById('app');
    //获得父级节点
    let parentNode = app.parentNode //body
    console.log(parentNode);
    //获得当前元素的所有直接子节点 是数组 用循环遍历
    let nodeList = app.childNodes
    console.log(nodeList);
    for (let i = 0; i < nodeList.length; i++) {
        let node = nodeList[i];
        console.log('name=' + node.nodeName, 'type=' + node.nodeType, 'value=' + node.nodeValue);
    }
    //第一个子节点
    let firstChild = app.firstChild;
    console.log('name=' + firstChild.nodeName, 'type=' + firstChild.nodeType, 'value=' + firstChild.nodeValue);
    //最后一个节点
    let lastChild = app.lastChild;
    console.log('name=' + lastChild.nodeName, 'type=' + lastChild.nodeType, 'value=' + lastChild.nodeValue);
    //前一个节点
    let h1 = lastChild.previousSibling;
    console.group('------------------');
    console.log(h1);
    console.group('------------------');
    //后一个节点
    let img;
    for (let i = 0; i < nodeList.length; i++) {
        if (nodeList[i].nodeType === 1 && nodeList[i].nodeName === 'P') {
            let p = nodeList[i];
            img = p.nextSibling.nextSibling
            break;
        }
    }
    console.log(img);
    img.src = "../image/4.jpg"

    //获得app中的所有元素节点 数组
    console.group('---------元素节点---------');
    let elementNode = app.children
    for (let i = 0; i < elementNode.length; i++) {
        let children = elementNode[i];
        console.log(children);
    }


</script>

</html>